/* 
 * File:   Insert.cpp
 * Author: maximiliano
 * 
 * Created on March 26, 2013, 5:43 PM
 */

#include "Insert.h"

using namespace TP3::Mangling;

Insert::Insert(int pos, string& value){
    _pos = pos;
    _value = value;
}

Insert::~Insert(){}

string& Insert::run (string& operating){
    
    if (_pos < 0){
        
        // Insertar a posterior.
        
        if (operating.length() <= 1)
            operating += _value;

        else if (_pos + (long int) operating.length() < 0)
            operating = operating.insert (1, _value);
        
        else if (_pos == -1)
            operating += _value;
        
        else
            operating = operating.insert (_pos + (long int) operating.length() + 1, _value);
    }
    else
    {
        // Se inserta en anterior.
        
        if (operating.length() <= 1)
            operating = _value + operating;
        
        else if (_pos >= (long int) operating.length())
            operating = operating.insert (operating.length() - 1, _value);
        
        else
            operating = operating.insert (_pos, _value);
    }
    
    return operating;
}

string Insert::getName(){
    
    return "insert";
}